Encrypt using rsa
Encrypt data using the RSA algorithm. Syntax: encrypt source using rsa with {public | private} key tKey passphrase tPassPhrase Examples: encrypt myMessage with public key myKey encrypt thisMessage with private key privateKey Use the encrypt using rsa command to encrypt a message using RSA public key encryption. The it variable contains the encrypted message. Parameters: *The source is the message to be encrypted *The tKey is the key to be used for the encryption, in PEM format *The tPassPhrase is an optional passphrase Use the form encrypt message with public key tkey to encode a message that you only want to be decoded by the holder of the private key. Use the form encrypt message with private key tkey to encode a message that a receiver can then verify has come from one of the holders of the private key (this is a signing operation). Generating key pairs Public-private key pairs can be generated using the OpenSSL suite of command-line tools. OpenSSL must be installed on your computer. For example: put shell("openssl genrsa -out private_key.pem 512") put shell("openssl rsa -pubout -in private_key.pem -out public_key.pem") Will generate a key pair of size 512-bits, placing the private key in private_key.pem and the public key in public_key.pem. For more information on these utilities see http://www.openssl.org/docs/apps/rsa.html and http://www.openssl.org/docs/apps/genrsa.html. The maximum length of a message that can be encrypted using RSA is the size of the key in bytes -11. So, for a 512-bit key pair, the maximum encryptable message size is 53 bytes. For signing, the maximum length of an encryptable message isn't really an issue since typically in that scenario it will be some sort of hash that would be being encrypted. For the more traditional encrypting scenario, however, the standard approach is to use public key cryptography to encrypt a random password which is then used with a symmetric cipher to actually encrypt the payload. See Also: decrypt Command, encrypt Command, decrypt using rsa Command Notes There are many ways to encrypt data, using mathematical methods called 'ciphers'. As clever as the ciphers are, so are the code breakers, and eventually they come up with mathematics that render the cipher useless - ie., it becomes relatively easy to guess the password/passphrase used to encode the data. At the moment, the 'AES-256' cipher is considered to be 'safe'. One particularly good thing about it, is that it is quite quick to encode data with, even for large amounts of data. One bad thing about it, is that it is what is called a 'symmetrical' encryption cipher, meaning that the same 'key' (a password or passphrase) is used to both encrypt and decrypt the data. In order to increase the security of the encryption, you can also use a 'salt' - a small, random sequence of characters - that will further 'scramble' the encrypted data. But that means that in order to decrypt it, you will need both the password and the salt at the other end. To encrypt any data with AES-256 in LiveCode: # put the data to be encrypted into a variable, like 'tData' # put the password or passphrase into a variable, like tPassword # put 8 random characters into a variable, like tSalt # encrypt the data with: encrypt tData using "aes256" with password tPassword and salt tSalt To decrypt the same data: # put the encrypted data into a variable - tData # put the password or passphrase into a variable - like tPassword # put the same salt characters into tSalt # decrypt the data with: decrypt tData using "aes256" with password tPassword and salt tSalt THE PASSWORD PROBLEM The problem with symmetrical encryption, however, is that the key must be known in order for you to encode and decode the message. This means, that if you want to use this to send/receive information in your programs, both the sender and receiver will have to know the key. If you hard-code the key into your program, a hacker can try to decompile your code and retrieve the key from it. The fact that you have to know or keep the password at hand, is just not safe. So, the problem is: I do want to send my encrypted data using AES-256, as it is quite safe. But I don't want to have the sender or the receiver know the password beforehand, or have to keep it in the system - all of which cause major security holes. ENCRYPTING PASSWORDS WITH RSA Another method of encrypting data is using private and public keys. In this kind of encryption, the public key can be distributed publicly and seen by everyone, because you cannot guess the private key by the public key alone. The public key is used to encrypt data that you want to be seen only by the holder of the private key. The private key can then be used to decrypt the data - the public key cannot do that. The private key can also be used to 'sign' data, so that people with the public key can confirm that the data really came from the private key holder. The problem with public-private key encryption is that, due to the mathematics involved, you can only encrypt a relatively small amount of data - an amount smaller than the size of your key. Although you could, in theory, 'break down' the data to be encrypted into encodable chunks, you would not do this for two reasons: this kind of encryption is slow, and doing this makes it much easier for hackers to guess your private key. So, you should only really encrypt a small amount of data with this kind of encryption - like, for instance, a password... LiveCode uses RSA for public-private key encryption. You need to generate a public and private key pair, first. The LiveCode dictionary has instructions on how to do that using the command line. The instructions are easy to follow, but I would recommend that you create a key that is 4096 bits, not 512 - a key that small is considered insecure nowadays. In order to encrypt a password/passphrase with RSA in LiveCode: # put the password/passphrase into a variable, like tPassword # put the public key into a variable, such as tPublicKey # encrypt the password with: encrypt tPassword using rsa with public key tPublicKey In order to decrypt the password/passphrase using the private key: # put the encrypted password into a variable - tPassword # put the private key into a variable, like tPrivateKey # decrypt the password: decrypt tPassword using rsa with private key tPrivateKey PUTTING IT ALL TOGETHER Using both encryption methods together, we can now send any amount of data with security. Let's say that Alice wants to send a document securely to Bob. She can do it like this: # Alice creates a random password - it really doesn't matter what it is - and a random salt. # Using the random password and salt, Alice encrypts her document using AES-256. # Alice has Bob's public key. Using RSA, she then encodes the *password* and *salt* with his public key. # Alice sends Bob the data containing: the RSA-encrypted password+salt and the AES-encrypted document. In order for Bob to receive it, he does this: # Bob separates the RSA-encrypted data (encrypted password+salt) from the AES-encrypted one (document). # Bob decrypts the password and salt using his RSA private key. # Using the decrypted password and salt, Bob now decrypts the document using AES. Note that neither Alice nor Bob need to even know what the password and salt are, and these values can be disposed of right after they are used. This ensures that there are new passwords and salts being used all the time, which makes it more difficult for hackers to break your security. FINAL CONCERNS All encryption functions produce BINARY data, which is not safe to transmit over the internet - the data may get 'misinterpreted' or corrupted by servers along the way. In order to make binary data safe to include in your internet transmissions, you need to encode it first. LiveCode has base64Encode and base64Decode functions, which are great for that. Data in base64 format is wrapped at 72 characters, which mean that your data may end up having several return characters. This may or may not cause issues, depending on your application - you may need to further encode your data to get rid of these returns - ie., by using the URLEncode and URLDecode functions. Category:Script Category:Encryption